<?php

/**
 * 同步旧的好物清单以及好物主题数据 到新的清单专题
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
ini_set("error_reporting", E_ALL);//显示所有错误

require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\Album\AlbumListModel;
use Biz\model\Album\AlbumWikiListModel;
use Biz\model\NiceGoodsTopic\GoodsList;
use Biz\model\NiceGoodsTopic\SecondTopic;
use Biz\model\wiki\MGoodsAlbum;
use Doraemon\cache\AlbumCounter;
use Doraemon\pockets\IdSender\IdSender;

//查询历史所有的好物清单数据
$goodsAlbum = MGoodsAlbum::select('*', ['status' => MGoodsAlbum::STATUS_NORMAL]);
$uid = AlbumListModel::DEFAULT_UID_PRODUCT;
if ($goodsAlbum) {
    foreach ($goodsAlbum as $item) {
        $albumId = IdSender::getAlbumId($uid);
        $data = [
            'album_id' => $albumId,
            'uid' => $uid,
            'title' => $item['title'],
            'desc' => $item['subtitle'],
            'publish_time' => strtotime($item['start_time']),
            'admin_id' => $item['admin_id'],
            'status' => AlbumListModel::STATUS_SUCCESS,
            'addtime' => date('Y-m-d H:i:s',$item['addtime']),
        ];
        $wikiList = explode(',', $item['wiki']);
        //查询标题是否存在
        $isExit = AlbumListModel::getByWhere(['title' => $item['title'], 'status' => AlbumListModel::STATUS_SUCCESS]);
        if ($isExit) {
            //不做处理
        } else {
            $id = AlbumListModel::insertData($data);
            AlbumCounter::increaseAlbumCounter($albumId , AlbumCounter::ACT_READ,rand(500,2000));
            if ($id) {
                $wikiList = explode(',', $item['wiki']);
                foreach ($wikiList as $wikiId) {
                    AlbumWikiListModel::insertData([
                        'album_id' => $albumId,
                        'uid' => $uid,
                        'wiki_id' => $wikiId,
                    ]);
                }

            }
        }

    }
    echo '好物清单历史数据导入完成'.PHP_EOL;
}
//查询历史所有的好物专题数据
$niceGoods = SecondTopic::select('*', ['topic_status' => SecondTopic::STATUS_NORMAL]);
if ($niceGoods) {
    foreach ($niceGoods as $goods) {
        $albumId = IdSender::getAlbumId($uid);
        $albumData = [
            'album_id' => $albumId,
            'uid' => $uid,
            'title' => $goods['topic_name'],
            'desc' => $goods['desc'],
            'cover_img' => $goods['img'],
            'publish_time' => strtotime($goods['show_time']),
            'admin_id' => $goods['admin_id'],
            'status' => AlbumListModel::STATUS_SUCCESS,
            'addtime' => $goods['addtime'],
        ];
        $isExit = AlbumListModel::getByWhere(['title' => $goods['topic_name'], 'status' => AlbumListModel::STATUS_SUCCESS]);
        if ($isExit) {
            //不做任何操作
        } else {
            $id = AlbumListModel::insertData($albumData);
            AlbumCounter::increaseAlbumCounter($albumId , AlbumCounter::ACT_READ,rand(500,2000));
            if ($id) {
                //查询关联的商品
                $goodsList = GoodsList::getListByWhere(['rela_topic_id' => $goods['id']]);
                if ($goodsList) {
                    foreach ($goodsList as $wikiInfo) {
                        AlbumWikiListModel::insertData([
                            'album_id' => $albumId,
                            'uid' => $uid,
                            'wiki_id' => $wikiInfo['wiki_id'],
                            'obj_id' => $wikiInfo['obj_id'],
                            'obj_type' => $wikiInfo['obj_type'],
                            'content' => $wikiInfo['content_desc'],
                        ]);
                    }
                }
            }
        }
    }
    echo '好物专题数据导入完成'.PHP_EOL;
}
